﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using YouHP.BLL.MedicalGraph;
using YouHP.Model;
using YouHP.BLL;
using System.Collections.Generic;
using YouHP.Utility;

namespace web.admin
{
    public partial class GraphLabcheck : System.Web.UI.Page
    {

        protected GraphSetting LabcheckSetting;
        protected DiseaseGraphSettingInfo SettingInfo;
        protected List<DiseaseLabcheckInfo> LabcheckList;
        protected int GraphIndex;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                HandleGet();
            }
            else
            {
                HandlePost();
            }
        }

        protected void HandleGet()
        {
            int dszid = RequestTools.GetQueryInt("id", -1);
            HiddenDiseaseId.Value = dszid.ToString();
            if (dszid < 0)
            {
                //this.AddErrorMessage("无效的参数");
                return;
            }
            GetSetting(dszid);
            BindAll();
        }

        protected void HandlePost()
        {
            int dszid = Utils.StrToInt(HiddenDiseaseId.Value, -1);
            if (dszid < 0) return;
            GetSetting(dszid);
        }

        protected void GetSetting()
        {
            int dszid = Utils.StrToInt(HiddenDiseaseId.Value, -1);
            if (dszid < 0) return;
            GetSetting(dszid);
        }

        public void GetSetting(int dszid)
        {
            SettingInfo = DiseaseBLL.Instance.GetDiseaseGraphSettingInfo(dszid);
            try
            {
                LabcheckSetting = SerializationHelper.DeserializeFromXml<GraphSetting>(SettingInfo.GraphLabcheckSetting);
            }
            catch
            {
                LabcheckSetting = new GraphSetting();
            }
            if (SettingInfo.DiseaseId <= 0) SettingInfo.DiseaseId = dszid;
            if (LabcheckSetting.ParamList.Count == 0) LabcheckSetting.ParamList.Add(new GraphRule());
            LabcheckList = DiseaseBLL.Instance.GetDiseaseLabcheckList(dszid);
        }
        public void SaveSetting()
        {
            SettingInfo.GraphLabcheckSetting = SerializationHelper.SerializeToXml(LabcheckSetting);
            if (SettingInfo.Id <= 0) DiseaseBLL.Instance.AddDiseaseGraphSetting(SettingInfo);
            else DiseaseBLL.Instance.UpdateDiseaseGraphSetting(SettingInfo);
        }

        protected void RefreshData()
        {
            SaveSetting();
            GetSetting();
            BindAll();
        }

        protected void BindAll()
        {
            BindGraph();
            BindLabcheck();
        }

        protected void BindLabcheck()
        {
            LsbLabcheckList.Items.Clear();
            foreach (DiseaseLabcheckInfo info in LabcheckList)
            {
                LsbLabcheckList.Items.Add(new ListItem(info.LabcheckName, info.LabcheckId.ToString()));
            }
        }

        protected void BindGraph()
        {
            LsbGraphList.Items.Clear();
            int i = 0;
            foreach (GraphRule item in LabcheckSetting.ParamList)
            {
                LsbGraphList.Items.Add(new ListItem(item.Name,i.ToString()));
                i++;
            }
        }

        #region Graph

        protected void BtnAddGraph_Click(object sender, EventArgs e)
        {
            GraphRule rule = new GraphRule();
            rule.Name = "新建图表";
            LabcheckSetting.ParamList.Add(rule);
            RefreshData();
        }

        protected void BtnDelGraph_Click(object sender, EventArgs e)
        {
            int idx = LsbGraphList.SelectedIndex;
            if (idx < 0) return;
            LabcheckSetting.ParamList.RemoveAt(idx);
            RefreshData();
        }

        protected void BtnUpGraph_Click(object sender, EventArgs e)
        {
            int idx = LsbGraphList.SelectedIndex;
            if (idx < 1) return;
            GraphRule rule = LabcheckSetting.ParamList[idx];
            LabcheckSetting.ParamList.RemoveAt(idx);
            LabcheckSetting.ParamList.Insert(idx-1, rule);
            RefreshData();
        }

        protected void BtnDownGraph_Click(object sender, EventArgs e)
        {
            int idx = LsbGraphList.SelectedIndex;
            if (idx < 0 || idx >= LsbGraphList.Items.Count - 1) return;
            GraphRule rule = LabcheckSetting.ParamList[idx+1];
            LabcheckSetting.ParamList.RemoveAt(idx+1);
            LabcheckSetting.ParamList.Insert(idx, rule);
            RefreshData();
        }

        protected void BtnRenameGraph_Click(object sender, EventArgs e)
        {
            int idx = LsbGraphList.SelectedIndex;
            if (idx < 0) return;
            GraphRule rule = LabcheckSetting.ParamList[idx];
            rule.Name = TxbGraphRename.Text;
            RefreshData();
        }

        protected void BtnSetMessage_Click(object sender, EventArgs e)
        {
            int idx = LsbGraphList.SelectedIndex;
            if (idx < 0) return;
            GraphRule rule = LabcheckSetting.ParamList[idx];
            rule.Message = TxbGraphMessage.Text;
            RefreshData();
        }

        protected void LsbGraphList_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            LsbVarList.Items.Clear();
            int gidx = LsbGraphList.SelectedIndex;
            GraphIndex = gidx;
            if (gidx < 0) return;
            
            GraphRule rule = LabcheckSetting.ParamList[gidx];
            if (rule.mGroups.Count == 0)
            {
                rule.mGroups.Add(new GraphRule.Group());
                SaveSetting();
            }
            GraphRule.Group group = rule.mGroups[0];
            int i = 0;
            foreach (GraphRule.Variable var in group.mVariables)
            {
                LsbVarList.Items.Add(new ListItem(var.mName,i.ToString()));
                i++;
            }
        }

        #endregion

        #region Var

        protected void BtnAddVar_Click(object sender, EventArgs e)
        {
            int gidx = LsbGraphList.SelectedIndex;
            if (gidx < 0) return;
            int labIdx = LsbLabcheckList.SelectedIndex;
            if (labIdx < 0) return;
            GraphRule rule = LabcheckSetting.ParamList[gidx];
            GraphRule.Group group = rule.mGroups[0];
            GraphRule.Variable var = new GraphRule.Variable();
            DiseaseLabcheckInfo info = LabcheckList[labIdx];
            var.mName = info.LabcheckName;
            var.mKey = info.LabcheckId.ToString();
            group.mVariables.Add(var);
            RefreshData();
        }

        protected void BtnDelVar_Click(object sender, EventArgs e)
        {
            int gidx = GraphIndex;
            if (gidx < 0) return;
            int vidx = LsbVarList.SelectedIndex;
            if (vidx < 0) return;
            GraphRule rule = LabcheckSetting.ParamList[gidx];
            GraphRule.Group group = rule.mGroups[0];
            group.mVariables.RemoveAt(vidx);
            RefreshData();
        }

        protected void BtnUpVar_Click(object sender, EventArgs e)
        {
            int gidx = LsbGraphList.SelectedIndex;
            if (gidx < 0) return;
            int vidx = LsbVarList.SelectedIndex;
            if (vidx < 1) return;
            GraphRule rule = LabcheckSetting.ParamList[gidx];
            GraphRule.Group group = rule.mGroups[0];
            GraphRule.Variable var = group.mVariables[vidx];
            group.mVariables.RemoveAt(vidx);
            group.mVariables.Insert(vidx - 1, var);
            RefreshData();
        }

        protected void BtnDownVar_Click(object sender, EventArgs e)
        {
            int gidx = LsbGraphList.SelectedIndex;
            if (gidx < 0) return;
            GraphRule rule = LabcheckSetting.ParamList[gidx];
            GraphRule.Group group = rule.mGroups[0];
            int vidx = LsbVarList.SelectedIndex;
            if (vidx < 0 || vidx >= group.mVariables.Count - 1) return;
            GraphRule.Variable var = group.mVariables[vidx+1];
            group.mVariables.RemoveAt(vidx+1);
            group.mVariables.Insert(vidx, var);
            RefreshData();
        }

        #endregion

    }
}
